<?php

class Medewerker_model extends CI_Model {
	
	var $adresId;
	var $contactgegevensId;
	
	public function getMedewerkers(){
		$this->db->select('*');
		$data = $this->db->get('medewerker');
		if($data->result()){
			return $data->result();
		}else {
			return null;
		}
	}
	
	public function getClientsByMedewerker($id){
		$this->db->select('*');
		$this->db->where('datum', date("Y-m-d"));
		$this->db->where('medewerker_id', $id);
		$this->db->join('client', 'client.client_id = taak.client_id');
		
		$data = $this->db->get('taak');
		
		if($data->result()){
			return $data->result();
		}else {
			return null;
		}
	}
	
	
	public function newMedewerker(){
		$insert = $this->db->insert('contactgegevens', $this->arrayContactData());
		$this->contactgegevensId = $this->db->insert_id();
			
		$insert = $this->db->insert('adres', $this->arrayAdresData());
		$this->adresId = $this->db->insert_id();
			
		$insert = $this->db->insert('medewerker', $this->arrayMedewerkerData());
	}
	
	public function updateMedewerker(){
		$this->contactgegevensId = $this->uri->segment(6);
		$this->db->where('contactgegevens_id', $this->contactgegevensId);
		$this->db->update('contactgegevens', $this->arrayContactData()); 
		
		$this->adresId = $this->uri->segment(5);
		$this->db->where('adres_id', $this->adresId);
		$this->db->update('adres', $this->arrayAdresData()); 
		
		$Medewerker_id = $this->uri->segment(4);
		$this->db->where('medewerker_id', $Medewerker_id);
		$this->db->update('medewerker', $this->arrayMedewerkerData()); 
	}
	
	public function editMedewerker($id){
		return $this->getMedewerker($id);
	}
	
	public function deleteMedewerker($id){
		$this->db->where('medewerker_id', $id);
		$this->db->delete('medewerker');
	}
	
	public function getMedewerker($id = null){
		$this->db->select('*');
		$this->db->join('adres', 'adres.adres_id = medewerker.adres_id', 'inner');
		$this->db->join('contactgegevens', 'contactgegevens.contactgegevens_id = medewerker.contactgegevens_id', 'inner');
		
		if (!$id == null){
			$this->db->where('medewerker_id', $id);
		}
		
		$data = $this->db->get('medewerker');
		
		if($data->result()){
			return $data->result();
		}else {
			return null;
		}
		
		
	}
	
	private function arrayContactData(){
		$contactData = array(
			'vaste_telefoon_nr' => $this->input->post('vaste_telefoon_nr'),
			'email' => $this->input->post('email'),
			'mobiel_telefoon_nr' => $this->input->post('mobiel_telefoon_nr')
		);
		
		return $contactData;
	}
	
	private function arrayAdresData(){
		$adresData = array(
			'straat' => $this->input->post('straat'),
			'huisnummer' => $this->input->post('huisnummer'),
			'woonplaats' => $this->input->post('woonplaats'),
			'postcode' => $this->input->post('postcode'),
			'wijk' => $this->input->post('wijk')
		);
		
		return $adresData;
	}
	
	private function arrayMedewerkerData(){
		$medewerkerData = array(
			'voornaam' => $this->input->post('voornaam'),
			'achternaam' => $this->input->post('achternaam'),
			'adres_id' => $this->adresId,
			'contactgegevens_id' => $this->contactgegevensId						
		);
		
		return $medewerkerData;
	}
	
}